﻿using ApiBridge.ZlWebReference;
using JTG.ServiceInterface.Factory;
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;

namespace ApiBridge
{
    class Program
    {
        /// <summary>
        /// 德阳人民医院专用接口
        /// </summary>
        /// <param name="args">
        /// 0:德阳人民医院HIS；
        /// 1:url；
        /// 2:内容;
        /// 3:webservice的action
        /// 4:目前参数4传：DocumentAccess
        /// 5:文件名
        /// </param> 
        static void Main(string[] args)
        {
            WebRequest.DefaultWebProxy = null;

            var logName = $"error{DateTime.Now:yyyy-MM-dd}.log";
            Stopwatch sw = new Stopwatch();
            sw.Start();
            var tmpStr = new StringBuilder($"\r\n请求开始时间:{DateTime.Now:yyyy-MM-dd HH:mm:ss}传入参数：{string.Join(",", args)}");

            var isTest = ConfigurationManager.AppSettings["IsTest"];

            if (isTest == "true")
            {
                //测试
#if DEBUG
                args = new string[4];
#endif
                //http://192.168.28.203:6998/services/HIPService?wsdl
                //http://192.168.28.120:7000/HIPService.asmx?op=HIPMessageServer
                //http://192.168.28.203:6998/services/HIPService.HIPServiceHttpSoap12Endpoint

                args[0] = "德阳人民医院His";
                args[1] = File.ReadAllText("url.txt");
                args[2] = File.ReadAllText("request.txt");
            }

            try
            {
                //将提交内容写入本地
                //File.WriteAllText("url.txt", args[1]);
                //File.WriteAllText("request.txt", args[2]);

                var apiResult = "";
                tmpStr.AppendLine($"接口开始前：{sw.ElapsedMilliseconds}");
                if (args.Length == 5)
                {
                    #region 服务引用方式

                    //读取用户名密码配置
                    var userName = ConfigurationManager.AppSettings["UserName"];
                    var pwd = ConfigurationManager.AppSettings["Password"];

                    using (var service = new HIPService
                    {
                        AuthenticationValue = new Authentication()
                        {
                            appsys_id = userName,
                            appsys_token = pwd
                        }
                    })
                    {
                        var paras = args[2].Split('|');
                        var para = string.Format(File.ReadAllText("DocumentAccess.txt"), paras[0], Guid.NewGuid().ToString(), paras[1]);
                        apiResult = service.HIPMessageServer(args[3], para);
                        //apiResult = service.HIPMessageServer(args[3], args[2]);
                    }

                    #endregion

                    #region HTTP方式

                    //http方式
                    //string _apiUrl = "http://192.168.70.241:8086/ExternalServices/ZL_InformationService.asmx/Information";

                    //var apiResult1 = SoapHelper.GetSoapReSource(_apiUrl, body, "application/x-www-form-urlencoded");

                    #endregion

                    #region 反射方式

                    //反射方式调用
                    //var hisBridgeService = new HisBridgeFactory().CreatePrintService(args[0].ToUpper());
                    //hisBridgeService.Request(args[1], para, "HIPService", "HIPMessageServer", "DocumentAccess");

                    #endregion


                    //将返回内容写入本地
                    File.WriteAllText(args[4], apiResult);
                }
                else
                {
                    //初始化实例
                    var hisBridgeService = new HisBridgeFactory().CreatePrintService(args[0].ToUpper());

                    //调用接口
                    apiResult = hisBridgeService.Request(args[1], args[2]);

                    //将返回内容写入本地
                    File.WriteAllText($"result.txt", apiResult);
                }
                tmpStr.AppendLine($"请求完成：{sw.ElapsedMilliseconds}");

            }
            catch (Exception e)
            {
                File.AppendAllText(logName,$"错误信息：{e.Message},{e.StackTrace}");
            }

            File.AppendAllText(logName, tmpStr.ToString());
        }

        private static string Utf8ToGb2312(string str)
        {
            var u8 = Encoding.UTF8;
            var gb = Encoding.GetEncoding("gb2312");
            var u8Bytes = u8.GetBytes(str);
            var convert = Encoding.Convert(u8,gb,u8Bytes);
            return gb.GetString(convert);
        }
    }
}